package org.example.controller;

import org.apache.commons.io.IOUtils;
import org.example.annotations.ProcessInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

@RestController()
@RequestMapping("*")
@CrossOrigin("*")
public class DownLoadController {

    @Value("${imageLocaltion}")
    private String fileLocation;

    @GetMapping("/downLoad")
    @ProcessInfo("下载")
    public void downLoad(HttpServletResponse resp){
        String fileName = "mi.png";
        String fullPath = fileLocation + fileName;

        //中文名字的用这一个
        //fileName = URLEncoder.encode(fileName,"utf-8")

        resp.setHeader("content-disposition","attachment;filename"+fileName);

        try {
            //写回文件
            IOUtils.copy(new FileInputStream(fullPath),resp.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
